package com.wz.other.test;

public class Foo {

	private volatile Bar mirror_bar = null;

	private Bar source_bar = null;

	public Foo(){
		source_bar = new Bar();
		mirror_bar = source_bar;
	}

	public void inc(){
		source_bar.setCount(source_bar.getCount()+1);
	}

	public Integer getNewCount() {
		return mirror_bar.getCount();
	}

	public static void main(String[] args) {
		final Foo foo = new Foo();
		new Thread(new Runnable() {

			@Override
			public void run() {
				for (int i = 0; i < 10; i++) {
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					foo.inc();
					System.out.println("增加!");
				}
			}
		}).start();

		new Thread(new Runnable() {

			@Override
			public void run() {
				for (int i = 0; i < 10; i++) {
					try {
						Thread.sleep(2000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					System.out.println("查询:" + foo.getNewCount());
				}
			}
		}).start();
	}
}